﻿package mortal.game.scene3D.model.pools
{
    import __AS3__.vec.*;
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import com.gengine.debug.*;
    import flash.system.*;
    import flash.utils.*;
    import frEngine.animateControler.*;
    import frEngine.manager.*;
    import frEngine.pools.*;
    import mortal.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.scene3D.display3d.icon3d.*;
    import mortal.game.scene3D.display3d.text3d.dynamicText3d.*;
    import mortal.game.scene3D.model.player.*;

    public class EffectPlayerPool extends Object
    {
        private var _creareList:Dictionary;
        private var createNum:int = 0;
        private var _preTime:Number = 0;
        private var _timesNum:int = 0;
        private var disposeNum:int = 0;
        private static var pools:Dictionary = new Dictionary();
        private static var _instance:EffectPlayerPool;
        private static var _defaultEffect:EffectPlayer;

        public function EffectPlayerPool()
        {
            this._creareList = new Dictionary(false);
            Resource3dManager.instance.autoToDispose = false;
            _defaultEffect = new EffectPlayer(null, null);
            _defaultEffect.enabledPool = false;
            return;
        }// end function

        public function removeObjectFromPool(param1:EffectPlayer) : void
        {
            var _loc_2:* = pools[param1.effectUrl];
            var _loc_3:* = _loc_2.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                if (_loc_2[_loc_4] == this)
                {
                    _loc_2.splice(_loc_4, 1);
                    return;
                }
                _loc_4++;
            }
            return;
        }// end function

        public function disposeAll() : void
        {
            var _loc_7:EffectPlayer = null;
            var _loc_8:String = null;
            var _loc_9:Array = null;
            var _loc_10:String = null;
            var _loc_11:int = 0;
            var _loc_12:Vector.<Vector.<Md5Matrix3D>> = null;
            var _loc_1:int = 0;
            var _loc_2:Array = [];
            var _loc_3:Array = [];
            Rect3DManager.instance.getInRenderTexture(_loc_2, _loc_3);
            var _loc_4:* = new Dictionary(false);
            var _loc_5:Array = [];
            var _loc_6:* = Resource3dManager.instance.staticResourceDic;
            for each (_loc_7 in this._creareList)
            {
                
                if (_loc_7.isDisposed)
                {
                    continue;
                }
                _loc_8 = _loc_7.effectUrl;
                if (_loc_7.renderList || _loc_6[_loc_8] != null)
                {
                    if (_loc_4[_loc_8] == null)
                    {
                        _loc_4[_loc_8] = true;
                        Resource3dManager.renderingTexture(_loc_7, _loc_2, _loc_3);
                        _loc_10 = GameScene3dConfig.instance.getEffectUrlById(_loc_8);
                    }
                    _loc_1++;
                    continue;
                }
                _loc_7.disposeTime = 0;
                _loc_7.isInPool = false;
                _loc_7.dispose(false);
                _loc_9 = pools[_loc_8];
                if (_loc_9)
                {
                    _loc_11 = _loc_9.indexOf(_loc_7);
                }
            }
            Icon3DFactory.instance.checkToDispose();
            Text3DFactory.instance.checkToDispose();
            Resource3dManager.instance.checkToDispose(null, _loc_2, _loc_3);
            if (System.totalMemoryNumber / 1024 / 1024 > 500)
            {
                _loc_12 = Md5SkinAnimateControler.Matrix3DDictionaryPool;
                if (_loc_12.length > 500)
                {
                    _loc_12.length = 500;
                }
            }
            Log.system("totalEffect:" + FrObjectPool._effectCreateNum, "leftEffect:" + _loc_1);
            Log.system("in render effectPlayers:", _loc_5.join(","));
            return;
        }// end function

        public function getPool(param1:String) : Array
        {
            var _loc_2:Array = null;
            if (param1 in pools)
            {
                _loc_2 = pools[param1];
            }
            else
            {
                var _loc_3:* = new Array();
                _loc_2 = new Array();
                pools[param1] = _loc_3;
            }
            return _loc_2;
        }// end function

        public function checkNoDispose() : void
        {
            var _loc_2:EffectPlayer = null;
            var _loc_1:Array = [];
            for each (_loc_2 in this._creareList)
            {
                
                if (_loc_2.disposeTime == 0)
                {
                    _loc_1.push(_loc_2);
                }
            }
            return;
        }// end function

        public function getEffectPlayer(param1:String, param2:Pivot3D, param3:RenderList, param4:Boolean, param5:Boolean = true, param6:Boolean = true) : EffectPlayer
        {
            var _loc_8:EffectPlayer = null;
            var _loc_9:Number = NaN;
            var _loc_10:Array = null;
            var _loc_11:int = 0;
            if (param6 == false)
            {
                _loc_9 = getTimer();
                if (_loc_9 - this._preTime < 500)
                {
                    if (this._timesNum > 20)
                    {
                        return _defaultEffect;
                    }
                    var _loc_12:String = this;
                    var _loc_13:* = this._timesNum + 1;
                    _loc_12._timesNum = _loc_13;
                }
                else
                {
                    if (this._timesNum > 20 && _loc_9 - this._preTime < 1000)
                    {
                        return _defaultEffect;
                    }
                    this._preTime = _loc_9;
                    this._timesNum = 0;
                }
            }
            var _loc_12:String = this;
            var _loc_13:* = this.createNum + 1;
            _loc_12.createNum = _loc_13;
            if (param3 == null)
            {
                param3 = Global3D.scene.renderLayerList;
            }
            var _loc_7:* = this.getPool(param1);
            if (_loc_7.length > 0)
            {
                _loc_8 = _loc_7.shift();
                _loc_8.isInPool = false;
                if (_loc_8.isDisposed)
                {
                    throw new Error("出错，特效已回收！");
                }
            }
            else
            {
                _loc_10 = FrObjectPool.getPool(EffectPlayer);
                _loc_11 = _loc_10.length;
                _loc_8 = FrObjectPool.getObject(EffectPlayer, this, null, param3);
                if (_loc_11 == 0)
                {
                    this._creareList[_loc_8] = _loc_8;
                }
            }
            _loc_8.reset();
            if (!_loc_8.loaded)
            {
                _loc_8.load(param1);
            }
            _loc_8.impRemove = param5;
            if (_loc_8.renderList != param3)
            {
                _loc_8.renderList = param3;
            }
            _loc_8.clearEnabled = param4;
            return _loc_8;
        }// end function

        public function dispose(param1:EffectPlayer) : Boolean
        {
            if (param1.effectUrl == null || param1.isDisposed || !param1.enabledPool)
            {
                return false;
            }
            var _loc_2:* = this.getPool(param1.effectUrl);
            var _loc_3:* = _loc_2.indexOf(param1);
            if (_loc_3 == -1)
            {
                _loc_2.push(param1);
                param1.disposeTime = getTimer();
                param1.isInPool = true;
                var _loc_4:String = this;
                var _loc_5:* = this.disposeNum + 1;
                _loc_4.disposeNum = _loc_5;
                return true;
            }
            return false;
        }// end function

        public function getPoolMsg() : String
        {
            var _loc_3:* = undefined;
            var _loc_4:Array = null;
            var _loc_1:String = "";
            var _loc_2:int = 0;
            for (_loc_3 in pools)
            {
                
                _loc_4 = this.getPool(_loc_3);
                _loc_2 = _loc_2 + _loc_4.length;
                _loc_1 = _loc_1 + (_loc_3 + ":<font color=\'#00FF00\'>" + _loc_4.length + "</font>\n");
            }
            return "EffectPlayerPool总对象数量：<font color=\'#00FF00\'>" + _loc_2 + "</font>\n" + _loc_1;
        }// end function

        public static function get instance() : EffectPlayerPool
        {
            if (!_instance)
            {
                _instance = new EffectPlayerPool;
            }
            return _instance;
        }// end function

    }
}
